﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Core;
using eBlog.Entity;
using eBlog.Common;
using System.Collections;
namespace eBlog.Web.Admin
{
    public partial class TagsManage : PageBase
    {
        IDBBase dal = DBFactory.GetDbProvider(); //初始化数据工厂
        protected void Page_Load(object sender, EventArgs e)
        {
            PageValidate.CheckAdminLogin(this);

            if (!Page.IsPostBack)
            {
                switch (action)
                {
                    case "add":
                    case "edit":
                        ShowInfo();
                        BindList();
                        break;
                    case "del":
                        delInfo();
                        break;
                    case "show":
                        //show.Visible = true;
                        break;
                    case "reset":
                        ResetTags();
                        BindList();
                        break;
                    default:
                       BindList();
                        break;
                }
            }
            else
            {
                if (action == "del")
                    delInfo();
            }
        }

        private void BindList()
        {
            int PageIndex;
            if (!int.TryParse(Request.Params["page"], out PageIndex)) { PageIndex = 1; }

            ArrayList attrs = dal.Where("Type=2").OrderBy("DisplayOrder").Page(PageIndex, PageSize).GetListAndCount<Terms>();
            var list = dal.Page(PageIndex, PageSize).GetListAndCount<Terms>();
            List<Terms> lists = attrs[0] as List<Terms>;
            ListCount = (int)attrs[1];
            RPLIST.DataSource = lists;
            RPLIST.DataBind();
            PageNav.Text = Pager.PageInfo(ListCount, PageSize, PageIndex);
        }


        public void btnSave_Click(object sender, EventArgs e)
        {
            ArrayList strErrs = new ArrayList(); //Add时候必须用::分开控件名
            this.edit.Visible = true;
            if (this.txtName.Text == "")
            {
                strErrs.Add("txtName::Name不能为空，请输入字符！");
            }
          
            if (this.txtDescription.Text == "")
            {
                strErrs.Add("txtDescription::Description不能为空，请输入字符！");
            }
            if (!PageValidate.IsNumber(txtDisplayOrder.Text))
            {
                strErrs.Add("txtDisplayOrder::DisplayOrder必须输入数字整数！");
            }

            if (strErrs.Count > 0)
            {
                string[] arrayErrs = (string[])strErrs.ToArray(typeof(string));
                string strErr = string.Join("\\n", arrayErrs);
                MessageBox.Show(this,strErr);

                return;
            }

            string Name = this.txtName.Text;
            string Description = this.txtDescription.Text;
            int DisplayOrder = int.Parse(this.txtDisplayOrder.Text);
            int Count = 0;
            DateTime Createdate = DateTime.Now;


            if (action == "edit" && id > 0)
            {
                Terms model = dal.GetModel<Terms>(id); //为了不覆盖更新,先读出存在的数据再按需填充数据

                model.Name = Name;
              
                model.Description = Description;
                model.DisplayOrder = DisplayOrder;
                model.Count = Count;
                model.Type = 2;
                model.Createdate = Createdate;
                dal.Update(model);
                MessageBox.ShowAndRedirect(this, "修 改 完 成!", Referrer.Value);
                return;
            }
            else
            {
                Terms model = new Terms();  //添加数据,准备新的实体

                model.Name = Name;
               
                model.Description = Description;
                model.DisplayOrder = DisplayOrder;
                model.Count = Count;
                model.Type = 2;
                model.Createdate = Createdate;


                Response.Redirect(Request.Url.AbsolutePath + "#" + dal.Add<Terms>(model).ToString()); //ADD并 返回当前Uri绝对地址不带参数
                return;
            }

        }

        private void ShowInfo()
        {
            try { this.Referrer.Value = Request.UrlReferrer.PathAndQuery; }
            catch { }
            if (id > 0 && action == "edit")
            {
                Terms model = dal.GetModel<Terms>(id);
                this.txtName.Text = model.Name;
              
                this.txtDescription.Text = model.Description;
                this.txtDisplayOrder.Text = model.DisplayOrder.ToString();
                this.btnSave.Text = "编 辑";
            }

        }

        private void delInfo()
        {
            string ids = PageValidate.Form("checkallid");
            if (!String.IsNullOrEmpty(ids))
            {
                //dal.From("vt_PostType", "").Where("id in (@ids)").SetString("ids", ids).Delete();
                dal.From("Terms", "").Where("id in (" + ids + ")").Delete();
            }
            else
            {
                dal.Delete<Terms>(id);
            }

            Response.Redirect(Request.Url.AbsolutePath); //返回当前Uri绝对地址不带参数
            return;
        }


        private void ResetTags()
        {
            var terms = dal.Where("Type=2").OrderBy("DisplayOrder").GetList<Terms>();
            foreach (var term in terms)
            {
                int tagsId = term.ID;
                var count = dal.From("Posts", "id")
                    .Where("Tag like '%," + tagsId + ",%' or Tag like '%," + tagsId + "'or Tag like'" + tagsId +",%' or Tag ='" + tagsId + "'")
                    .GetCount();
                term.Count = count;
                dal.Update(term);

            }
        }


    }
}
